﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.DB;
using MySql.Data.MySqlClient;
using MaxLeaf.Common;

namespace MaxLeaf.DAL.Web
{
    /// <summary>
    /// 分发基类,add/update/delete
    /// </summary>
    public class BaseDAL : MaxLeaf.DAL.BaseDALBuilder.IDALAction
    {
        public enum GetDataType
        {
            OrgData

        }

        protected string strTableName = null;
        protected string strKeyFieldName = null;
        //除key以外需要维护的字段
        protected string strFieldNames = null;

        public virtual void Delete(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {

            string strSql =string.Format( @"delete from {0} where  {1}=?{1}",strTableName,strKeyFieldName);
            MySqlParameter[] arrParam = new[] { new MySqlParameter(string.Format("?{0}",strKeyFieldName), strKeyValue) };
            dbCon.ExecuteSql(strSql, arrParam);



        }

        public virtual void Add(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {

            //DataRow drData = dbCon.GetTableDataFirstRow( "mary_goods", new Dictionary<string, object> { { "FItemID", strKeyValue } }, "FItemID");

            //if (drData != null)
            //{
            //    Update(dbCon, drvLog);
            //    return;
            //}

            //string strSql = "insert into mary_goods(FItemID,is_on_sale)  values(?FItemID,0)";
            //MySqlParameter[] arrParam = new MySqlParameter[] { new MySqlParameter("?FItemID", drvLog[DEL.LogDEL.KeyValue]) }; 
            //dbCon.ExecuteSql( strSql, arrParam );

            Update( dbOrgCon,dbCon, strKeyValue);

        }

        /// <summary>
        /// 每次全部update
        /// </summary>
        /// <param name="dbOrgCon">来源数据库</param>
        /// <param name="dbDestCon">目标数据库</param>
        /// <param name="strKeyValue"></param>
        public virtual void Update(DBAccess dbOrgCon, DBAccess dbDestCon, string strKeyValue)
        {
            string strUpdateFieldNames = strFieldNames;        
            
            DataTable dtData = dbDestCon.GetTableData(strTableName, new Dictionary<string, object> { { strKeyFieldName, strKeyValue } }, strKeyFieldName + "," + strUpdateFieldNames);
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;
            if (dtData.Rows.Count == 0)
            {
                logStatus = LogStatus.Add;
                drData = dtData.NewRow();
                drData[strKeyFieldName] = strKeyValue;

                strUpdateFieldNames = strKeyFieldName + "," + strUpdateFieldNames;
            }
            else
            {
                drData = dtData.Rows[0];
            }


            
            DataTable dtOrgData = DAL_GetData(dbOrgCon, dbDestCon, (int)GetDataType.OrgData, strKeyValue);
            if (dtOrgData == null || dtOrgData.Rows.Count == 0)
            {
                return;
            }
            DataRow drK3OldData = dtOrgData.Rows[0];
            string[] arrFieldName = strUpdateFieldNames.Split(',');
            foreach (string strField in arrFieldName)
            {
                DAL_GetFieldData(dbOrgCon, dbDestCon, strField, drK3OldData, drData);
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateParamSql(strUpdateFieldNames, strTableName, drData, logStatus, strKeyFieldName);

            dbDestCon.ExecuteSql(kvp.Key, kvp.Value);


        }
        /// <summary>
        /// 根据FielName赋值
        /// </summary>
        /// <param name="dbOrgCon"></param>
        /// <param name="dbDestCon"></param>
        /// <param name="strFieldName"></param>
        /// <param name="drOrgData"></param>
        /// <param name="drDestData"></param>
        protected virtual void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            drDestData[strFieldName] = drOrgData[strFieldName];
        }

        protected virtual DataTable DAL_GetData(DBAccess dbOrgCon, DBAccess destCon, int dataType, object objParam)
        {




            throw new MaxLeafException(string.Format("GetData {0} 没注册", dataType));
        }

    }
}
